package com.extremelylargepackage;

import java.io.IOException;
import java.io.InputStream;
import java.util.Dictionary;
import java.util.HashMap;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.extremelylargepackage.entity.EntityFactory;
import com.extremelylargepackage.entity.EntityFactory.EntityType;
import com.extremelylargepackage.entity.Projectile;
import com.extremelylargepackage.sprite.AbstractSprite;

import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.util.Log;

public class ProjectileParser 
{
	public static HashMap<String, Projectile> loadProjectiles(AssetManager a)
	{
		HashMap<String, Projectile> projectiles = new HashMap<String, Projectile>();
		String[] paths = null;
		try 
		{
			paths = a.list("projectile");
			for(int i = 0; i < paths.length; i++)
			{
				Log.d("", paths[i]);
				InputStream stream = a.open("projectile/" + paths[i]);
				Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(stream);
				doc.getDocumentElement().normalize();
				
				Projectile proj = (Projectile)EntityFactory.createEntity(EntityType.projectile);
				
				Element element = (Element) doc.getElementsByTagName("Projectile").item(0);
				String name = element.getAttribute("name");
				float speed = Float.parseFloat(element.getAttribute("speed")) * GameResources.SCREEN_HEIGHT;
				AbstractSprite sprite = GameResources.getResource(element.getAttribute("sprite"));
				float damage = Float.parseFloat(element.getAttribute("damage"));
				int pierceDepth = 1; 
				String pierce = element.getAttribute("pierceDepth");
				if(pierce != null & pierce != "")
				{
					pierceDepth = Integer.parseInt(pierce);
				}
				
				EntityFactory.setProjectileProperties(proj, speed, sprite, damage, pierceDepth);
				EntityFactory.setName(proj, name);
				projectiles.put(name, proj);
			}
		}
		catch(Exception e)
		{
			Log.e("ERROR PARSING PROJECTILE", "", e);
		}
		return projectiles;
	}
}
